import cv2 as cv
import numpy as np



cap=cv.VideoCapture(0,cv.CAP_DSHOW)
cap.set(3,960)#设置视频参数与参数
cap.set(4,960)
cap.set(cv.CAP_PROP_SATURATION, 50)  #设置饱和度
cap.set(cv.CAP_PROP_CONTRAST,40)  #设置对比度

# cap.set(cv.CAP_PROP_FRAME_WIDTH,1280)
# cap.set(cv.CAP_PROP_FRAME_HEIGHT,720)
cap.set(cv.CAP_PROP_FPS, 30)
cap.set(cv.CAP_PROP_BRIGHTNESS,120)


def beauty_face(img):
    dst = np.zeros_like(img)
    #int value1 = 3, value2 = 1; 磨皮程度与细节程度的确定
    v1 = 3
    v2 = 1
    dx = v1 * 5 # 双边滤波参数之一 
    fc = v1 * 12.5 # 双边滤波参数之一 
    p = 0.1
    temp4 = np.zeros_like(img)

    temp1 = cv.bilateralFilter(img,dx,fc,fc)
    temp2 = cv.subtract(temp1,img);
    temp2 = cv.add(temp2,(10,10,10,128))
    temp3 = cv.GaussianBlur(temp2,(2*v2 - 1,2*v2-1),0)
    temp4 = cv.add(img,temp3)
    dst = cv.addWeighted(img,p,temp4,1-p,0.0)
    dst = cv.add(dst,(10, 10, 10,255))
    return dst

def readvideo(videopath):
    cap = cv.VideoCapture("test/test.mp4")  # 打开视频
    while cap.isOpened():
        ret, fram = cap.read()  # 读取视频返回视频是否结束的bool值和每一帧的图像
        cv.imshow('a', fram)
        cv.waitKey(1)
        # cv2.destroyAllWindows()



videocap = cv.VideoCapture("video/s.mp4")
videocap.set(cv.CAP_PROP_FPS, 30)
fps=1/30
wait=int(fps*1000)
flag=1 #设置一个标识，用来输出视频信息
while(cap.isOpened()):
    ret,frame=cap.read()

    vret, vfram = videocap.read()
    src_mask = 255 * np.ones(vfram.shape, vfram.dtype)
    frame=beauty_face(frame)  #美颜
    # #rotoed=roto(frame)  #扣绿幕
    width, height, channels = frame.shape
    center= (int(height / 2), int(width / 2))
    output = cv.seamlessClone(vfram, frame, src_mask, center, cv.MIXED_CLONE)
    cv.imshow("Frame",output)
    # cv.imshow("Frame", vfram)
    k=cv.waitKey(wait)&0xFF #每帧数据延迟1ms,延迟不能为0，否则读取的结果是静态帧
    if k ==ord('s'):
       print(cap.get(3))
       print(cap.get(4))
    elif k==ord('q'):
       break

cap.release()
cv.destroyAllWindows()